import { ElMessage, ElMessageBox } from 'element-plus';
import { TimeDifferenceUtil } from '@/util/common_utils';
import { clearObj } from '@/util/sys_dict_const';
import axios from 'axios';
import { useRouter } from 'vue-router';
import { SignUtil } from '@/util/signin';
const router = useRouter();
const ServerCache = {};
const getUserServer = () => {
	const key = '/bussServer'; // 注意此处为了实现跨域，必须在vite.config.ts中配置跨域代理，用代理中的值代替模板服务器地址
	if (ServerCache[key]) {
		return ServerCache[key];
	} else {
		let server = axios.create({
			baseURL: key,
			timeout: 2000,
			withCredentials: true //该值表示携带cookie向服务端发送请求
		});
		ServerCache[key] = server;
		return server;
	}
};
const generateHeader = () => {
	let _headers = {};
	_headers['Access-Control-Allow-Origin'] = '*';
	_headers['Authorization'] = SignUtil.getAccessToken();
	_headers['Accept'] = '*/*';
	_headers['Connection'] = 'keep-alive';
	return _headers;
};
const appendJsonSubmitHeader = header => {
	if (header) {
		header['Content-Type'] = 'application/json;charset=UTF-8';
	}
};
export const UserServerUtil = {
		logout: async () => {
			let _server = getUserServer();
			const _userInfor=SignUtil.getUserSigninInfo();
			const _token=SignUtil.getAccessToken();
			if(!_userInfor){
				return;
			}
			let _data = {
				userId: _userInfor["userId"],
				logId:_userInfor["signInLogId"]
			};
			let _headers = generateHeader();
			appendJsonSubmitHeader(_headers);
			//console.log(_data);
			let url = '/logout/';
			await _server
				.get(url, _data, {
					headers: _headers
				})
				.catch(error => {
					console.log(error);
				});
		},
	findUserById: async (userId) => {
		let _server = getUserServer();
		let _data = {
			
		};
		let _headers = generateHeader();
		appendJsonSubmitHeader(_headers);
		//console.log(_data);
		let url = '/user/find_one/'+userId;
		await _server
			.post(url, _data, {
				headers: _headers
			})
			.then(res => {
				console.log(res);
				let code = res.data['code'];
				if (code == 200) {
					let _isSupM=res.data['supermanager'];
					console.log(res.data);
				} else {
					console.log(res.data);
				}
			})
			.catch(error => {
				console.log(error);
			});
	},
};
